/**
 * 
 */
package com.java.controller.admin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.java.entity.Course;
import com.java.entity.Users;
import com.java.service.impl.CollegeServiceImpl;
import com.java.service.impl.CourseServiceImpl;
import com.java.service.impl.StudentServiceImpl;
import com.java.service.impl.TeacherSeriveImpl;

/**
* @author Kidney
* 创建时间：2019年4月29日 下午3:04:51
* Description:
*/
/**
 * @author KIDNEY
 *
 */
@Controller
public class CourseController {

	@Autowired
	private CourseServiceImpl cs;

	@Autowired
	private CollegeServiceImpl cos;

	@Autowired
	private TeacherSeriveImpl ts;

	@Autowired
	private StudentServiceImpl ss;

	@RequestMapping(value = "all_course", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String allCourse(HttpServletResponse response) {

		List<Course> listCourse = cs.getAll();

		JSONArray array = new JSONArray();

		List<Map<String, Object>> alist = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < listCourse.size(); i++) {
			JSONObject jsonObj = new JSONObject();
			// SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
			// Date currentTime = listCourse.get(i).getTime();
			// String dateString = formatter.format(currentTime);

			HashMap<String, Object> map = new HashMap<String, Object>();

			map.put("id", listCourse.get(i).getCourseid());
			map.put("department", cos.getCollegeName(listCourse.get(i).getCollegeid()));
			map.put("classroom", "第" + listCourse.get(i).getClassroom() + "教室");
			map.put("name", listCourse.get(i).getCoursename());
			map.put("day", listCourse.get(i).getCoursetime());
			map.put("type", listCourse.get(i).getCoursetype());
			map.put("teacher", ts.getTeacherName(listCourse.get(i).getTeacherid()));
			// int t = Integer.parseInt(dateString.substring(0, 2));

			String courseTime = "";

			if (listCourse.get(i).getTime() > 0 && listCourse.get(i).getTime() < 10) {
				courseTime = "0" + listCourse.get(i).getTime() + ":00:00";
			} else
				courseTime = listCourse.get(i).getTime() + ":00:00";

			map.put("time", courseTime);
			map.put("hour", listCourse.get(i).getHour() + "小时");

			// jsonObj.put("course", map);
			array.add(map);
			// System.out.println(cos.getCollegeName(listCourse.get(i).getCollegeid()));
			// System.out.println("第" + listCourse.get(i).getClassroom() +
			// "教室");
			// System.out.println(listCourse.get(i).getCoursename());
			// System.out.println(listCourse.get(i).getCoursetime());
			// System.out.println(listCourse.get(i).getCoursetype());
			// System.out.println(ts.getTeacherName(listCourse.get(i).getTeacherid()));
			// System.out.println(dateString);
			// System.out.println("----------------------");

		}

		response.setContentType("application/json;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		// String jsonString = JSON.toJSONString(alist);

		return array.toJSONString();
	}

	public String getCourse(HttpServletResponse response) {
		List<String> alist = new ArrayList<String>();
		for (int n = 0; n < 10; n++) {
			alist.add("");
		}
		List<Course> listCourse = cs.getAll();
		JSONArray array = new JSONArray();
		for (int i = 0; i < 5; i++) {
			JSONObject json = new JSONObject();
			if (i == 1) {
				for (int x = 0; x < listCourse.size(); x++) {
					if ("周一" == listCourse.get(x).getCoursetime()) {
						// SimpleDateFormat formatter = new
						// SimpleDateFormat("HH:mm:ss");
						// Date currentTime = listCourse.get(x).getTime();
						// String dateString = formatter.format(currentTime);
						// String s = dateString.substring(2);
						// Integer it = new Integer(s);
						// int a = it.intValue();
						alist.set(1, "hehehe");
					}
				}
			}
			json.put("course", alist);
			String str = JSON.toJSONString(json, SerializerFeature.DisableCircularReferenceDetect);
			JSONObject jsonjson = JSON.parseObject(str);
			array.add(jsonjson);
		}
		return array.toJSONString();
	}

	@RequestMapping("admin_course")
	public String password() {

		return "pages/admin/courseHome.jsp";
	}

	@RequestMapping(value = "allcourse", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String adminCourse(HttpServletResponse response) {

		List<Course> listCourse = cs.getAll();

		String jsonString = JSON.toJSONString(listCourse);

		response.setContentType("application/json;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		return jsonString;
	}

	@RequestMapping(value = "delete", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Integer deleteCourse(Integer id) {
		int flag = cs.delete(id);
		return flag;
	}

	@RequestMapping(value = "insert", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Integer insertCourse(String name, Integer teacherId, String courseTime, String classroom, String type,
			Integer collegeId, Integer courseweek, Integer time) {

		Course course = new Course();
		course.setClassroom(classroom);
		course.setCollegeid(collegeId);
		course.setCoursename(name);
		course.setCoursetime(courseTime);
		course.setCoursetype(type);
		course.setCourseweek(courseweek);
		course.setTeacherid(teacherId);

		// 注意：SimpleDateFormat构造函数的样式与strDate的样式必须相符

		course.setTime(time);

		int flag = cs.insert(course);
		return flag;
	}

	@RequestMapping(value = "update", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Integer updateCourse(Integer courseid, String name, Integer teacherId, String courseTime, String classroom,
			String type, Integer collegeId, Integer courseweek, Integer time) {

		Course course = new Course();
		course.setClassroom(classroom);
		course.setCollegeid(collegeId);
		course.setCoursename(name);
		course.setCoursetime(courseTime);
		course.setCoursetype(type);
		course.setCourseweek(courseweek);
		course.setTeacherid(teacherId);
		course.setTime(time);

		course.setCourseid(courseid);
		int flag = cs.update(course);
		return flag;
	}

	@RequestMapping(value = "select_course", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String byCondition(HttpServletResponse response, String courseName, Integer courseid, Integer collgeid) {

		List<Course> listCourse = cs.byCondition(courseName, courseid, collgeid);

		JSONArray array = new JSONArray();

		List<Map<String, Object>> alist = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < listCourse.size(); i++) {
			JSONObject jsonObj = new JSONObject();

			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", listCourse.get(i).getCourseid());
			map.put("department", cos.getCollegeName(listCourse.get(i).getCollegeid()));
			map.put("classroom", "第" + listCourse.get(i).getClassroom() + "教室");
			map.put("name", listCourse.get(i).getCoursename());
			map.put("day", listCourse.get(i).getCoursetime());
			map.put("type", listCourse.get(i).getCoursetype());
			map.put("teacher", ts.getTeacherName(listCourse.get(i).getTeacherid()));
			String courseTime = "";
			if (listCourse.get(i).getTime() > 0 && listCourse.get(i).getTime() < 10) {
				courseTime = "0" + listCourse.get(i).getTime() + ":00:00";
			} else
				courseTime = listCourse.get(i).getTime() + ":00:00";
			map.put("time", courseTime);
			map.put("hour", listCourse.get(i).getHour() + "小时");
			array.add(map);
		}
		return array.toJSONString();
	}

	@RequestMapping(value = "student_course", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String studentCourse(HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();

		Users users = (Users) subject.getPrincipal();
		String userid = users.getUsername();
		int collgeid = ss.getCollege(Integer.parseInt(userid));
		List<Course> listCourse = cs.byCondition(null, null, collgeid);

		JSONArray array = new JSONArray();

		List<Map<String, Object>> alist = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < listCourse.size(); i++) {

			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", listCourse.get(i).getCourseid());
			map.put("department", cos.getCollegeName(listCourse.get(i).getCollegeid()));
			map.put("classroom", "第" + listCourse.get(i).getClassroom() + "教室");
			map.put("name", listCourse.get(i).getCoursename());
			map.put("day", listCourse.get(i).getCoursetime());
			map.put("type", listCourse.get(i).getCoursetype());
			map.put("teacher", ts.getTeacherName(listCourse.get(i).getTeacherid()));
			Integer courseTime = listCourse.get(i).getTime();
//			if (listCourse.get(i).getTime() > 0 && listCourse.get(i).getTime() < 10) {
//				courseTime = "0" + listCourse.get(i).getTime() + ":00:00";
//			} else
//				courseTime = listCourse.get(i).getTime() + ":00:00";
			map.put("time", courseTime);
			map.put("hour", listCourse.get(i).getHour());
			array.add(map);
		}
		return array.toJSONString();
	}

	
	@RequestMapping(value = "teacher_course", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String teacherCourse(HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();

		Users users = (Users) subject.getPrincipal();
		String userid = users.getUsername();

		List<Course> listCourse = cs.byTeacher(Integer.parseInt(userid));

		JSONArray array = new JSONArray();

		
		for (int i = 0; i < listCourse.size(); i++) {

			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", listCourse.get(i).getCourseid());
			map.put("department", cos.getCollegeName(listCourse.get(i).getCollegeid()));
			map.put("classroom", "第" + listCourse.get(i).getClassroom() + "教室");
			map.put("name", listCourse.get(i).getCoursename());
			map.put("day", listCourse.get(i).getCoursetime());
			map.put("type", listCourse.get(i).getCoursetype());
			map.put("teacher", ts.getTeacherName(listCourse.get(i).getTeacherid()));
//			String courseTime = "";
//			if (listCourse.get(i).getTime() > 0 && listCourse.get(i).getTime() < 10) {
//				courseTime = "0" + listCourse.get(i).getTime() + ":00:00";
//			} else
//				courseTime = listCourse.get(i).getTime() + ":00:00";
			map.put("time", listCourse.get(i).getTime());
			map.put("hour", listCourse.get(i).getHour());
			array.add(map);
		}
		return array.toJSONString();
	}
	
	
	@RequestMapping("teacher_timetable")
	public String teacher() {
		return "pages/user/teacher.jsp";
	}
	
	
	@RequestMapping("student_timetable")
	public String student() {
		return "pages/user/student.jsp";
	}
	
}
